﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using System.Text;
using System.Drawing;
using System.ComponentModel;
using System.Xml.Serialization;
using System.Windows.Forms;

namespace FicusFactor.Watermarker
{
    [Serializable]
    public class Settings
    {
        public const string SettingsDir = @"%APPDATA%\FicusFactor\watermarker";
        public const string SettingsFileName = "watermarkersettings.xml";

        public void Save()
        {
            string settingsDir = Environment.ExpandEnvironmentVariables(SettingsDir);
            string file = Path.Combine(settingsDir, SettingsFileName);
            XmlSerializer serializer = new XmlSerializer(GetType());
            Directory.CreateDirectory(settingsDir);
            using (StreamWriter fs = File.CreateText(file))
            {
                serializer.Serialize(fs, this);
            }
        }

        public static Settings Load()
        {
            string settingsDir = Environment.ExpandEnvironmentVariables(SettingsDir);
            string file = Path.Combine(settingsDir, SettingsFileName);
            if (false == File.Exists(file))
            {
                return new Settings();
            }

            try
            {
                XmlSerializer serializer = new XmlSerializer(typeof(Settings));
                using (FileStream fs = File.OpenRead(file))
                {
                    Settings s = (Settings)serializer.Deserialize(fs);
                    s.WasDeserialized = true;
                    return s;
                }
            }
            catch (Exception e)
            {
                MessageBox.Show("There was an error loading your settings. Your configuration has been lost. Sorry. :( Error: " + e.Message, "Error loading settings");
                return new Settings();
            }
        }

        [XmlIgnore]
        public bool WasDeserialized
        {
            get;
            private set;
        }

        public string FontName
        {
            get;
            set;
        }

        public float FontSize
        {
            get;
            set;
        }

        public bool FontSizeIsProportional
        {
            get;
            set;
        }

        public GraphicsUnit GraphicsUnit
        {
            get;
            set;
        }

        public FontStyle FontStyle
        {
            get;
            set;
        }

        public int Transparency
        {
            get;
            set;
        }

        public int SelectedColor
        {
            get;
            set;
        }

        public string WatermarkText
        {
            get;
            set;
        }

        public string OutputFolder
        {
            get;
            set;
        }

        public WatermarkPosition Position
        {
            get;
            set;
        }

        public int GetAlpha()
        {
            return (int)(((double)Transparency / 10) * 255);
        }

        public Font ToFont()
        {
            return new Font(FontName, FontSize, FontStyle, GraphicsUnit);
        }

        public Color ToColor()
        {
            return Color.FromArgb(SelectedColor);
        }

        public void FromColor(Color color)
        {
            SelectedColor = color.ToArgb();
        }

        public void FromFont(Font font)
        {
            FontName = font.Name;
            FontSize = font.Size;
            FontStyle = font.Style;
            GraphicsUnit = font.Unit;
        }

        public Settings()
        {
        }
    }
}